package cn.springbook.common.core.config.serializer;

import cn.springbook.common.core.config.serializer.LongToStringSerializer;
import cn.springbook.common.core.constant.LocalDateTimeConstant;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;


@Configuration
public class ObjectMapperConfig {



	/**
	 * 注解的方式需要在每个属性上进行标注，如果日期类属性较多则较为繁琐，自定义配置类方式可以对日期进行统一的格式化处理。
	 * 两者都存在的情况下，以注解为准，即注解方式会覆盖统一配置方式。
	 */
	@Bean
	@Primary
	public ObjectMapper objectMapper() {
		ObjectMapper objectMapper = new ObjectMapper();
		// 指定时区
		objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
		// 日期类型字符串处理
		objectMapper.setDateFormat(new SimpleDateFormat(LocalDateTimeConstant.DEFAULT_DATE_TIME_PATTERN));

		// Java8日期日期处理
		JavaTimeModule javaTimeModule = new JavaTimeModule();
		javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(LocalDateTimeConstant.DEFAULT_DATE_TIME_PATTERN)));
		javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(LocalDateTimeConstant.DEFAULT_DATE_PATTERN)));
		javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(LocalDateTimeConstant.DEFAULT_TIME_PATTERN)));
		javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(LocalDateTimeConstant.DEFAULT_DATE_TIME_PATTERN)));
		javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(LocalDateTimeConstant.DEFAULT_DATE_PATTERN)));
		javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(LocalDateTimeConstant.DEFAULT_TIME_PATTERN)));
		objectMapper.registerModule(javaTimeModule);

		//处理返回为long类型的数据
		SimpleModule simpleModule = new SimpleModule();
		simpleModule.addSerializer(Long.class, LongToStringSerializer.instance);
		simpleModule.addSerializer(Long.TYPE, LongToStringSerializer.instance);
		objectMapper.registerModule(simpleModule);

		objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		return objectMapper;
	}


}
